package com.huan.iteye.job;

import com.huan.iteye.analysis.repository.IteyeBlogTotalViewsRepository;
import com.huan.iteye.analysis.repository.IteyeBlogViewRepository;
import com.huan.iteye.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 删除冗余表数据
 *
 * @author huan.fu
 * @date 2018/10/9 - 14:38
 */
@Component
public class DeleteRedundancyTableDataJob extends AbstractCustomJob {

	@Autowired
	private IteyeBlogTotalViewsRepository blogTotalViewsRepository;
	@Autowired
	private IteyeBlogViewRepository blogViewRepository;

	@Override
	public void doExecute(String workDate) {
		LocalDate startDate = LocalDate.parse(workDate, DateTimeFormatter.ofPattern(BlogAnalysisResultEmailNotifyJob.TIME_FORMAT)).minusDays(1L);
		LocalDate endDate = startDate.minusDays(BlogAnalysisResultEmailNotifyJob.DATE_RANGE);
		List<String> rangeDates = DateUtil.calculateDateRange(endDate, startDate);
		blogTotalViewsRepository.deleteByWorkDateNotIn(rangeDates);
		blogViewRepository.deleteByWorkDateNotIn(rangeDates);
	}
}
